package com.onluy.frameworkcloud.batis.dialect;

public class MySQLDialect implements Dialect {
    public String getPagingSql(String sql, int start, int end) {
        StringBuilder str = new StringBuilder();
        str.append(sql);
        str.append(" LIMIT " + start + ", " + (end - start));
        return str.toString();
    }

    public String getCountSql(String sql) {
        return "SELECT COUNT(1) FROM (" + sql + ") AS CNT";
    }

    public String getToDate(String value) {
        return "str_to_date(\'" + value + "\',\'" + this.getDateFormat(value) + "\')";
    }

    public String getDateFormat(String value) {
        switch (value.length()) {
            case 4 :
                return "%Y";
            case 5 :
            case 9 :
            case 11 :
            case 12 :
            case 14 :
            case 15 :
            case 17 :
            case 18 :
            default :
                return "";
            case 6 :
                return "%Y%m";
            case 7 :
                return "%Y-%m";
            case 8 :
                return "%Y%m%d";
            case 10 :
                return "%Y-%m-%d";
            case 13 :
                return "%Y-%m-%d %H";
            case 16 :
                return "%Y-%m-%d %H:%i";
            case 19 :
                return "%Y-%m-%d %H:%i:%s";
        }
    }
}